package com.cyl.h5.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cyl.h5.domain.vo.H5ProductVO;
import com.cyl.h5.domain.vo.ProductDetailVO;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.service.CouponCategoryService;
import com.cyl.manager.pms.convert.ProductConvert;
import com.cyl.manager.pms.domain.entity.Product;
import com.cyl.manager.pms.domain.query.ProductQuery;
import com.cyl.manager.pms.service.ProductService;
import com.cyl.manager.ums.domain.entity.Member;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.config.LocalDataUtil;

@RestController
@RequestMapping("/h5/product")
public class H5ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductConvert productConvert;
    @Autowired
    private CouponCategoryService couponCategoryService;
    
    @PostMapping("/list")
    public ResponseEntity<Page<H5ProductVO>> queryGoodByPage(@RequestBody ProductQuery query, Pageable page) {
        Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        if(member==null || member.getDeptZbjId() == null || member.getDeptDpId() == null) {
            return ResponseEntity.ok(new PageImpl<>(new ArrayList<H5ProductVO>(), page, 0));
        }
        
        query.setDeptDpId(member.getDeptDpId());
        query.setDeptZbjId(member.getDeptZbjId());
        query.setPublishStatus(1);
        int js = member.getRolezd().intValue();
        if(js == 0) { // 0->普通用户；1->核销员；2->店长 3 副店长 4 群主
        	query.setMemberPublishStatus(1);
        }else 
		if(js == 1) {
			query.setHxyPublishStatus(1);
		}else 
		if(js == 2 || js == 3 || js == 4) {
			query.setShopPublishStatus(1); 	
		}
        List<Product> pageRes = productService.selectList(query, page);
        List<H5ProductVO> retH5ProductList =  productConvert.dos2dtos(pageRes);
        if(retH5ProductList!=null && retH5ProductList.size()>0) {
        	for (H5ProductVO h5ProductVO : retH5ProductList) {
        		 if(h5ProductVO.getCplx()!=null && h5ProductVO.getDhCouponCategoryId()!=null) {
                 	CouponCategory couponCategory = couponCategoryService.selectCheckById(h5ProductVO.getDhCouponCategoryId());
                 	if(couponCategory!=null) {
                 		h5ProductVO.setFlqmc(couponCategory.getTitle());
                 	}
                 }
			}
           
        }
        return ResponseEntity.ok(new PageImpl<>(retH5ProductList, page, ((com.github.pagehelper.Page) pageRes).getTotal()));
    }
    @GetMapping("/detail/{id}")
    public ResponseEntity<ProductDetailVO> queryDetail(@PathVariable Long id) {
        ProductDetailVO detail = productService.queryDetail(id);
        return ResponseEntity.ok(detail);
    }
}
